//
//  HSDrawerController.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/9/27.
//  Copyright © 2016年 赖虹宇. All rights reserved.
//

import UIKit

class HSDrawerController: UIViewController {
    
    //创建全局的左边视图控制器
    
    let leftViewVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LeftVc")
    //创建全局的tabBar视图控制器
     let tabBarVc = HSTabBarController()
   //动画时长
    let animationTime : TimeInterval = 0.5
    //标签视图的动画移动最大tx值
    let barOffset = screenW*0.842
    //左边视图的动画移动最大tx值
    let leftOffset = screenW / 2
    //两个视图移动最大值比例
    var offsetScale : CGFloat {
        return leftOffset / barOffset
    }
    
    //记录第一次触摸点
    var currentP : CGPoint?
    
    
    //懒加载遮盖视图
    lazy var coverView : UIButton = {
        //创建按钮
        let btn = UIButton(frame: UIScreen.main.bounds)
        //设置透明度
        btn.alpha = 0.0
        btn.backgroundColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
        //监听方法
        btn.addTarget(self, action: #selector(leftAnimation), for:.touchUpInside )
        
        return btn
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
       //添加抽屉效果的子控制器
        view.backgroundColor = UIColor.gray
        
        setUpUI()
 
        view.addSubview(tabBarVc.view)
        
        //添加拖拽手势
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGesture(gesture:)))
        view.addGestureRecognizer(panGesture)
        
       //获得导航左边按钮的监听事件
        tabBarVc.leftBtnBlock = {[weak self] () -> () in
            if self?.tabBarVc.view.transform.tx == 0 {
                //transform.tx 就是视图改变的X值  如果改变了就不是0 如果没改变就是0
                self?.rightAnimation()
            } else{
             self?.leftAnimation()
            }
        }
    }
    
    //手势回调方法
    @objc fileprivate func panGesture(gesture : UIPanGestureRecognizer){
        switch gesture.state {
        case .began:
            //当前点坐标
        currentP =  gesture.location(in: gesture.view)
          
        case .changed:
            //获得移动点的坐标
            let offset = gesture.translation(in: gesture.view)

            //重新设置视图的位置
            let newTx = tabBarVc.view.transform.tx + offset.x
//            print("视图的点变化\(tabBarVc.view.transform.tx)")
            if newTx >= screenW*0.842 {
                rightAnimation()
                return
            }
            if newTx <= 0 {
                leftAnimation()
                return
            }
            //设置左边视图移动值
            let leftNewTx = leftViewVc.view.transform.tx + offset.x * offsetScale
            tabBarVc.view.transform = CGAffineTransform(translationX:newTx, y: 0)
            leftViewVc.view.transform = CGAffineTransform(translationX: leftNewTx, y: 0)
            //恢复初始值
            gesture.setTranslation(CGPoint(), in: gesture.view)
            
        case .ended:
          //获得结束时的点
            let endp = gesture.location(in: gesture.view)
            if (currentP?.x)! < endp.x {
                rightAnimation()
            }else{
                leftAnimation()
            }
        default:
            break
            
        }
        
    }
    
    //设置UI--设置私有属性
  fileprivate  func setUpUI(){
        //左边视图
        //同时添加下面2个 才能完全关联再一起
        //添加控制器到管理控制器
        addChildViewController(leftViewVc)
        //设置frame
        leftViewVc.view.frame = CGRect(x: 0, y: 0, width: barOffset-3, height: screenH)
        //设置边界阴影
        leftViewVc.view.layer.shadowColor = UIColor.black.cgColor
        leftViewVc.view.layer.shadowOffset = CGSize(width: 3, height: 0)
        leftViewVc.view.layer.shadowOpacity = 0.9
        
        //同时添加视图
        view.addSubview(leftViewVc.view)
     //设置默认的形变偏移量
        leftViewVc.view.transform = CGAffineTransform(translationX: -self.leftOffset, y: 0)
        
        //添加标签控制器
        
        addChildViewController(tabBarVc)
        //设置frame
        tabBarVc.view.frame = CGRect(x: 0, y: 0, width: screenW, height: screenH)
        
    }
    
    //右边滑动动画方法
    func rightAnimation(){
        //添加遮盖视图--添加遮盖视图的过程不需要动画，只是透明度的改变在动画过程中需要改变
        self.tabBarVc.view.addSubview(self.coverView)
        UIView.animate(withDuration: animationTime, animations: {
            //动画移动视图
            self.tabBarVc.view.transform = CGAffineTransform(translationX: self.barOffset, y: 0)
            self.leftViewVc.view.transform = CGAffineTransform(translationX: 0, y: 0)
            //设置透明度
            self.coverView.alpha = 0.4
            })
    }
    //左边滑动动画
    func leftAnimation(){
        UIView.animate(withDuration: animationTime, animations: { 
            self.tabBarVc.view.transform = CGAffineTransform(translationX: 0, y: 0)
            
            self.leftViewVc.view.transform = CGAffineTransform(translationX: -self.leftOffset, y: 0)
            //取消遮盖视图-方法1
             self.coverView.alpha = 0
            }) { (_) in
                //取消遮盖视图  --方法2
//                self.coverView.removeFromSuperview()
                
                
        }
    }
}
